package com.vx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.activitiFlow.service.IHistoryBiz;
import com.activitiFlow.service.IRepositoryBiz;
import com.activitiFlow.service.IRuntimeBiz;
import com.activitiFlow.service.ITaskBiz;
import com.vx.FlowCode;
import com.vx.service.IBorrowService;
import com.vx.service.IProcessInstanceEntryService;
import com.vx.service.IUserService;
import com.vx.vo.BorrowVO;
import com.vx.vo.UserVO;

@Controller
@RequestMapping("/task")
public class TaskController {
	
	private String _userCode=null;
	private String _candidateUsers=null;
	
	@Autowired
	private IBorrowService borrowService;
	@Autowired
	private ITaskBiz taskBiz;
	@Autowired
	private IRuntimeBiz runtimeBiz;
	
	@Autowired
	private IRepositoryBiz repositoryBiz;
	
	@Autowired
	private IProcessInstanceEntryService processInstanceEntryService;
	
	/**
	 * 获取任务列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("list")
	public String startPross(Model model,HttpServletRequest request){
		UserVO user=(UserVO) request.getSession().getAttribute("user");
		List<Task> taskList=taskBiz.getRunTaskListByUserCode(user.getUserCode());
		model.addAttribute("taskList", taskList);
		return "workflow/task";
	}
	
	/**
	 * 连接到办理任务界面
	 * @return
	 */
	@RequestMapping("viewTaskFrom")
	public String viewTaskFrom(String taskId,Model model,HttpServletRequest request){
		String formUrl="workflow/taskForm";
		//获取当前运行节点指定的办理页面
//		TaskFormData formData=workflowService.getTaskFormData(taskId);
//		formUrl=formData.getFormKey();
		//查询任务信息,根据task信息中的processInstanceId从processInstance对象中获取业务相关的数据id
		String borrowId=runtimeBiz.getBusinessIdByTaskId(taskId);
		//3.查询业务系统的借阅单信息
		BorrowVO borrow=borrowService.findBorrowById(borrowId);
		//----  根据taskid获取当前流程对应的分支连线 ---
		List<String> lineNames=processInstanceEntryService.getLineNames(taskId);
		//查询历史的审批信息
		List<Comment> commentList=taskBiz.getHistCommentByTaskId(taskId);
		model.addAttribute("borrow",borrow);
		model.addAttribute("lineNames",lineNames);
		model.addAttribute("commentList",commentList);
		model.addAttribute("taskId",taskId);
		
		//获取历史审批信息
		return  formUrl;
	}
	
	/**
	 * 保存审批处理信息
	 * @param taskId
	 * @param businessid
	 * @param outcome
	 * @param comment
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("saveSubmitTask")
	public String saveSubmitTask(String taskId,String businessid,String outcome,String comment,Model model,HttpServletRequest request){
		UserVO user=(UserVO) request.getSession().getAttribute("user");
		_userCode=user.getUserCode();
		_candidateUsers="zuh";
		int status=taskBiz.submitTask(_userCode,taskId, outcome, comment,_candidateUsers);
		if(status==FlowCode.FLOW_END){
			//更新请假单表的状态从1变成2（审核中-->审核完成）
			BorrowVO borrow = new BorrowVO();
			borrow.setId(Long.valueOf(businessid));
			borrow.setStatus(2);
			borrowService.saveBorrow(borrow);
		}
		return "redirect:/task/list"; 
	}
	
	/**
	 * 查看审批流程审批情况图
	 * @param taskId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("viewRunTaskDeployImg")
	public String viewRunTaskDeployImg(String taskId,Model model,HttpServletRequest request){
		ProcessDefinition processDefinition=repositoryBiz.queryProcessDefinitionByTaskId(taskId);
		String deploymentId = processDefinition.getDeploymentId();
		String imgName=processDefinition.getDiagramResourceName();
		model.addAttribute("imgName", imgName);
		model.addAttribute("deploymentId", deploymentId);
		ActivityImpl activityNode=processInstanceEntryService.getRunActivityNode(taskId);
		Map<String,Object> coordMap=new HashMap<String, Object>();
		coordMap.put("x",activityNode.getX());
		coordMap.put("y",activityNode.getY());
		coordMap.put("width",activityNode.getWidth());
		coordMap.put("height",activityNode.getHeight());
		model.addAttribute("coordMap", coordMap);
		return "workflow/image"; 
	}
}
